home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / AddOns / Components / TEECHART / Src Code / BUBBLECH.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-10-24  |  5.7 KB  |  175 lines

  1. {**********************************************}
  2. {   TBubbleSeries (derived from TPointSeries)  }
  3. {   Copyright (c) 1995-1996 by David Berneda   }
  4. {**********************************************}
  5. unit BubbleCh;
  6.  
  7. interface
  8.  
  9. { TBubbleSeries derives from standard TPointSeries.
  10.   Each point in the series is drawn like a Bubble.
  11.   Each point has a Radius value that's used to draw the Bubble with its
  12.   corresponding screen size.
  13.  
  14.   Inherits all functionality from TPointSeries and
  15.   its ancestor (TCustomSeries).
  16. }
  17. Uses WinTypes,Classes,Graphics,Chart,Series,TeEngine; { <-- needed units }
  18.  
  19. Type
  20.   TBubbleSeries=class(TPointSeries)
  21.   private
  22.     FRadiusValues : TChartValueList; { <-- Bubble's radius storage }
  23.     FSquared      : Boolean;
  24.     Procedure SetSquared(Value:Boolean);
  25.     Procedure SetRadiusValues(Value:TChartValueList);
  26.     Function ApplyRadius( Const Value:Double;
  27.                           AList:TChartValueList;
  28.                           Increment:Boolean):Double;
  29.   protected
  30.     procedure DrawValue(ValueIndex:Longint); override; { <-- main draw method }
  31.   public
  32.     Constructor Create(AOwner: TComponent); override;
  33.     Function AddBubble(Const AX,AY,ARadius:Double; Const AXLabel:String; AColor:TColor):Longint;
  34.     Procedure Assign(Source:TPersistent); override;
  35.     Procedure DrawLegendShape(ValueIndex:Longint; Const Rect:TRect); override;
  36.     Procedure FillSampleValues(NumValues:Longint); override; { <-- to add random radius values }
  37.     Function IsValidSourceOf(Value:TChartSeries):Boolean; override;
  38.     Function MaxYValue:Double; override;  { <-- adds radius }
  39.     Function MinYValue:Double; override;  { <-- substracts radius }
  40.   published
  41.     property ColorEachPoint default True;
  42.     property RadiusValues:TChartValueList read FRadiusValues write SetRadiusValues;
  43.     property Squared:Boolean read FSquared write SetSquared default True;
  44.   end;
  45.  
  46. implementation
  47.  
  48. Uses SysUtils,TeeProcs,TeeConst,TeCanvas;
  49.  
  50. { TBubbleSeries }
  51. Constructor TBubbleSeries.Create(AOwner: TComponent);
  52. Begin
  53.   inherited Create(AOwner);
  54.   FRadiusValues:=TChartValueList.Create(Self,TeeMsg_ValuesBubbleRadius); { <-- radius storage }
  55.   With Pointer do
  56.   begin
  57.     InflateMargins:=False;
  58.     Style:=psCircle; { <-- a Bubble is a circle (by default) }
  59.     AllowChangeSize:=False;
  60.   end;
  61.   Marks.Frame.Visible:=False;
  62.   Marks.Transparent:=True;
  63.   FSquared:=True;
  64.   ColorEachPoint:=True;
  65. end;
  66.  
  67. Procedure TBubbleSeries.SetSquared(Value:Boolean);
  68. Begin
  69.   SetBooleanProperty(FSquared,Value);
  70. end;
  71.  
  72. Procedure TBubbleSeries.SetRadiusValues(Value:TChartValueList);
  73. Begin
  74.   SetChartValueList(FRadiusValues,Value); { standard method }
  75. end;
  76.  
  77. { Helper method, special to Bubble series }
  78. Function TBubbleSeries.AddBubble( Const AX,AY,ARadius:Double;
  79.                                   Const AXLabel:String; AColor:TColor):Longint;
  80. Begin
  81.   result:=AddXY(AX,AY,AXLabel,AColor); { standard add X,Y }
  82.   FRadiusValues.TempValue:=ARadius;
  83.   AddValue(result);
  84. end;
  85.  
  86. Procedure TBubbleSeries.FillSampleValues(NumValues:Longint);
  87. Var t     : Longint;
  88.     tmpX  : Double;
  89.     tmpY  : Double;
  90.     StepX : Double;
  91.     MinY  : Double;
  92.     DifY  : Double;
  93. Begin
  94.   Clear;
  95.   CalcRandomBounds(NumValues,tmpX,StepX,tmpY,MinY,DifY);
  96.   for t:=1 to NumValues do { some sample values to see something in design mode }
  97.   Begin
  98.     AddBubble( tmpX,                { X }
  99.                Random(Round(DifY)), { Y }
  100.                (DifY/10.0)+Round(DifY/(10+Random(15)))  { <- Radius }
  101.                {$IFNDEF D4},'', clTeeColor{$ENDIF});
  102.     tmpX:=tmpX+StepX;
  103.   end;
  104.   RefreshSeries;
  105. end;
  106.  
  107. procedure TBubbleSeries.DrawValue(ValueIndex:Longint);
  108. Var tmpSize : Longint;
  109. Begin
  110.   { This overrided method is the main paint for bubble points.
  111.     The bubble effect is achieved by changing the Pointer.Size based
  112.     on the corresponding Radius value for each point in the series.
  113.  
  114.     We dont use Pointer.Size:=... because that will force a repaint
  115.     while we are painting !! giving recursive endlessly repaints !!!
  116.   }
  117.   tmpSize:=CalcYSizeValue(FRadiusValues.Value[ValueIndex]);
  118.   if FSquared then
  119.      Pointer.ChangeHorizSize(tmpSize)
  120.   else
  121.      Pointer.ChangeHorizSize(CalcXSizeValue(FRadiusValues.Value[ValueIndex]));
  122.   Pointer.ChangeVertSize(tmpSize);
  123.   DrawPointer(CalcXPos(ValueIndex),CalcYPos(ValueIndex),ValueColor[ValueIndex],ValueIndex);
  124.   { dont call inherited }
  125. end;
  126.  
  127. Function TBubbleSeries.ApplyRadius( Const Value:Double;
  128.                                     AList:TChartValueList;
  129.                                     Increment:Boolean):Double;
  130. var t:Longint;
  131. begin
  132.   result:=Value;
  133.   for t:=0 to Count-1 do
  134.   if Increment then
  135.      result:=MaxDouble(result,AList[t]+FRadiusValues.Value[t])
  136.   else
  137.      result:=MinDouble(result,AList[t]-FRadiusValues.Value[t]);
  138. end;
  139.  
  140. Function TBubbleSeries.MaxYValue:Double;
  141. Begin
  142.   result:=ApplyRadius(inherited MaxYValue,YValues,True);
  143. end;
  144.  
  145. Function TBubbleSeries.MinYValue:Double;
  146. Begin
  147.   result:=ApplyRadius(inherited MinYValue,YValues,False);
  148. end;
  149.  
  150. Procedure TBubbleSeries.Assign(Source:TPersistent);
  151. begin
  152.   if Source is TBubbleSeries then
  153.      FSquared:=TBubbleSeries(Source).FSquared;
  154.   inherited Assign(Source);
  155. end;
  156.  
  157. Function TBubbleSeries.IsValidSourceOf(Value:TChartSeries):Boolean;
  158. begin
  159.   result:=Value is TBubbleSeries; { Only Bubbles can be assigned to Bubbles }
  160. end;
  161.  
  162. Procedure TBubbleSeries.DrawLegendShape(ValueIndex:Longint; Const Rect:TRect);
  163. var tmp : Longint;
  164. begin
  165.   tmp:=MinLong((Rect.Right-Rect.Left),(Rect.Bottom-Rect.Top));
  166.   Pointer.ChangeHorizSize(tmp);
  167.   Pointer.ChangeVertSize(tmp);
  168.   inherited DrawLegendShape(ValueIndex,Rect);
  169. end;
  170.  
  171. initialization
  172.   RegisterTeeSeries( TBubbleSeries, TeeMsg_GalleryBubble,
  173.                      TeeMsg_GalleryStandard,2);
  174. end.
  175.